import Koa from 'koa';
import * as esbuild from 'esbuild';
import fs from 'fs/promises';

const app = new Koa();

// 请求文件，例如 ./js/index.ts
// 用 esbuild.transform 转换 .ts 为 .js 内容
// 响应体为 fs 将字符串转换为文件
app.use(async (ctx, next) => {
  await next();
  console.log(ctx.request.url);
  // ctx.response.type = '.js';
  // ctx.response.body = file;
});

app.listen(82);

async function getTransformedFile(): Promise<string> {
  const initialFileContent = await fs.readFile('./js/index.ts', 'utf8');
  const transformedFile = await esbuild.transform(initialFileContent, {});
  return transformedFile.code;
}
